﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;

using Happy.Activities.Domain.Workflows;
using Happy.Activities.Domain.WorkflowInstances;

namespace Happy.Activities.Domain.Test
{
    [TestClass]
    public class SequenceWorkflowTest
    {
        [TestMethod]
        public void Sequence_Test()
        {
            var workflow = new Workflow
            {
                Id = Guid.NewGuid()
            };

            workflow.AddActivity(new ManualActivity
            {
                Id = Guid.NewGuid(),
                DisplayName = "A",
                IsFinal = false,
                Name = "A"
            }, true);
            workflow.AddActivity(new ManualActivity
            {
                Id = Guid.NewGuid(),
                DisplayName = "B",
                IsFinal = true,
                Name = "B"
            });

            workflow.AddRouter(new Router
            {
                Id = Guid.NewGuid(),
                DisplayName = "A->B",
                FromId = workflow["A"].Id,
                ToId = workflow["B"].Id
            });


            var instance = new WorkflowInstance(workflow);


            instance.Run(new Dictionary<string, object>());
            var activityInstances = instance.GetActivityInstances();
            var routerInstances = instance.GetRouterInstances();

            Assert.AreEqual(1, activityInstances.Length);
            Assert.AreEqual(ActivityState.Entered, activityInstances[0].State);
            Assert.AreEqual(WorkflowState.Running, instance.State);
            Assert.AreEqual(0, routerInstances.Length);


            instance.Resume(activityInstances[0].Id, new Dictionary<string, object>());
            activityInstances = instance.GetActivityInstances();
            routerInstances = instance.GetRouterInstances();

            Assert.AreEqual(2, activityInstances.Length);
            Assert.AreEqual(ActivityState.Exited, activityInstances[0].State);
            Assert.AreEqual(ActivityState.Entered, activityInstances[1].State);
            Assert.AreEqual(WorkflowState.Running, instance.State);
            Assert.AreEqual(1, routerInstances.Length);


            instance.Resume(activityInstances[1].Id, new Dictionary<string, object>());
            activityInstances = instance.GetActivityInstances();
            routerInstances = instance.GetRouterInstances();

            Assert.AreEqual(2, activityInstances.Length);
            Assert.AreEqual(ActivityState.Exited, activityInstances[0].State);
            Assert.AreEqual(ActivityState.Exited, activityInstances[1].State);
            Assert.AreEqual(WorkflowState.Completed, instance.State);
            Assert.AreEqual(1, routerInstances.Length);
        }
    }
}
